package com.dbq.fast.business.invite.api;

import com.dbq.fast.business.invite.been.InviteInfo;
import com.dbq.fast.business.invite.been.ShareIntroduce;
import com.dbq.fast.business.login.api.LoginApi;
import com.dbq.fast.business.product.api.CommonTransformer;
import com.dbq.fast.common.bean.User;
import com.dbq.fast.common.net.HttpManager;
import com.dbq.fast.common.net.RemoteApiCallBack;

import java.util.HashMap;

/**
 * 处理邀请页面的接口
 * 包含邀请界面信息和分享信息的接口
 */
public class InviteApi {

    /**
     * 获取用户分享的链接，返回值即为分享的字段
     *
     * @param c
     */
    public static void inviteShareLink(RemoteApiCallBack<InviteInfo> c) {
        User user = User.getInstance();
        HashMap hashMap = new HashMap();
        hashMap.put("moduleId", "1");
        hashMap.put("vpnId", String.valueOf(user.getVpnId()));
        HttpManager.getInstance().getHttpService()
                .shareLinkInfo(user.getToken(), hashMap)
                .compose(new CommonTransformer<>())
                .subscribe(httpResult -> {
                    if (httpResult.isSuccess()) {
                        c.onSuccess(httpResult.getData());
                    } else {
                        c.onFail(httpResult.getCode(), httpResult.getMsg());
                    }
                }, throwable -> {
                    c.onFail(400, throwable.getMessage());
                });
    }

    /**
     * 获取邀请界面展示的详细信息，包括规则，邀请提示，获利百分比。
     *
     * @param c
     */
    public static void inviteIntroduce(RemoteApiCallBack<ShareIntroduce> c) {
        HttpManager.getInstance().getHttpService()
                .shareRuleIntroduce("1")
                .compose(new CommonTransformer<>())
                .subscribe(httpResult -> {
                    if (httpResult.isSuccess()) {
                        c.onSuccess(httpResult.getData());
                    } else if(httpResult.getCode() == 401){
                        new Thread(()->{
                            String newToken = LoginApi.refreshTokenSync(true);
                            if ("".equals(newToken)) return;
                            inviteIntroduce(c);
                        }).start();

                    }else {
                        c.onFail(httpResult.getCode(), httpResult.getMsg());
                    }
                }, throwable -> {
                    c.onFail(400, throwable.getMessage());
                });
    }
}
